package luogu.GraphTheory;
//P3366 【模板】最小生成树
//空间超了
import java.util.Arrays;
import java.util.Scanner;

/**
 * @author :chenjie
 * @date :Created 2023/1/8 15:40
 */
public class MinimumSpanningTree {
    static int n,m,sum;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        m=sc.nextInt();
        int[][]arr=new int[n+1][n+1];
        for (int i = 1; i < n+1; i++) {
            Arrays.fill(arr[i],0x3f3f3f3f);
        }
        for (int i = 0; i < m; i++) {
            int a=sc.nextInt();
            int b=sc.nextInt();
            int c=sc.nextInt();
            arr[a][b]=Math.min(arr[a][b],c);
            arr[b][a]=Math.min(arr[b][a],c);
        }
        prim(arr);
        if(sum==0x3f3f3f3f){
            System.out.println("orz");
        }else {
            System.out.println(sum);
        }

    }
    public static void prim(int[][]arr){
        int[]dist=new int[n+1];
        boolean[]st=new boolean[n+1];
        Arrays.fill(dist,0x3f3f3f3f);
        for (int i = 0; i < n; i++) {
            int t=-1;
            for (int j = 1; j <= n; j++) {
                if(!st[j]&&(t==-1||dist[t]>dist[j])){
                    t=j;
                }
            }
            if(i!=0&&dist[t]==0x3f3f3f3f){
                sum=0x3f3f3f3f;
                return;
            }
            if(i>0){
                sum=sum+dist[t];
            }
            for (int j = 1; j <= n; j++) {
                dist[j]=Math.min(dist[j],arr[t][j] );
            }
            st[t]=true;
        }
    }
}
